package com.chinasofti.etc.alcohol.web;


import com.chinasofti.etc.alcohol.common.Result;
import com.chinasofti.etc.alcohol.dao.model.User;
import com.chinasofti.etc.alcohol.exceptions.BusinessException;
import com.chinasofti.etc.alcohol.service.ICommodityCartService;
import com.chinasofti.etc.alcohol.service.info.CartInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lucas
 * @since 2019-05-28
 */
@Controller
@Slf4j
public class CommodityCartController extends BaseController{

    @Autowired
    private ICommodityCartService cartService;

    /**
     * 此方法用于ajax添加数据但不跳转
     * @param commodityId
     * @param number
     * @return
     */
    @RequestMapping("cart/ajaxAdd")
    @ResponseBody
    public Result addCart(int commodityId,@RequestParam(defaultValue = "1")int number, HttpSession session) {
        log.info("添加到购物车，commodityId={},number={}",commodityId,number);
        try {
            //TODO 模拟用户登录后的情况下，写死1，应当从session获取，getUserId(session)
            User user=(User) session.getAttribute("user");
            cartService.add(commodityId, number,user.getUserId());
        }catch (BusinessException e){
            return Result.fail(e.getMessage());
        }catch (Exception e){
            //TODO 考虑用ControllerAdvice捕获未知异常，但要区分是否ajax
            log.warn("添加购物车异常",e);
            return Result.fail("系统异常，请稍后再试");
        }

        return Result.success();

    }

    /**
     * 此方法用于添加数据并且跳转到购物车页面
     * @return
     * @param session
     */
    @RequestMapping(value="/cart/add")
    public String addAndJump(int commodityId, @RequestParam(defaultValue = "1")int number,HttpSession session,ModelMap modelMap){
        Result r = addCart(commodityId,number,session);
        if(r.isSuccess()){
            return "redirect:/cart/";
        }else{
            //TODO 跳转到错误页，并提示信息
            return "error";
        }

    }

    /**
     * 此方法用于进入购物车页面
     * @return
     */
    @RequestMapping(value="/cart",method = RequestMethod.GET)
    public String toCart(HttpSession session,ModelMap modelMap){

        //TODO 模拟用户登录后的情况下，写死1，应当从session获取，getUserId(session)

        User user=(User) session.getAttribute("user");
        List<CartInfo> cartList = cartService.findAllCartItemByUserId(user.getUserId());
        modelMap.put("carts",cartList);

        return "cart";
    }


    @RequestMapping("cart/delete")
    @ResponseBody
    public Result delCart(int cartId, RedirectAttributes rs) {
        cartService.removeById(cartId);
        return Result.success();
    }
}